<?php
namespace app\admin\job;

use app\common\service\CloudService;
use app\api\service\RedisMsgService;
use app\common\service\ConfigService;
use app\common\service\HttpService;
use think\facade\Console;
use think\facade\Cache;
// use think\queue\Job;
use dengje\workerman\process\queue\Client as Job;
use think\facade\Log;
use app\admin\service\system\PluginService;



class DownloadJob
{
    private $process = null;



    public function fire($data)
    {
        try {
            $license = ConfigService::get('system', 'license');
            //....这里执行具体的任务 
            (new CloudService())->setProcessLog($data['client_id'], '进入下载队列');
            // if ($job->attempts() > 1) {
            //     //通过这个方法可以检查这个任务已经重试了几次了
            //     $job->delete();
            // }
            // 备份admin.php 防止更新失败无法再次更新
            $backupDir = runtime_path(). 'backup'. DIRECTORY_SEPARATOR. date('Ymd');
            $adminBackupFile = $backupDir. DIRECTORY_SEPARATOR. 'admin.php';

            if (!is_dir($backupDir)) {
                mkdir($backupDir, 0777, true);
            }
            copy(root_path(). 'config'. DIRECTORY_SEPARATOR .'admin.php',$adminBackupFile);

            $service = new CloudService();
            $file = $service->chunkDownload($data['chunk_files'], $data['client_id']);
            (new CloudService())->setProcessLog($data['client_id'], '解压文件');
            // 解压下载的文件
            unzip_file($file, root_path());

            // exec('php '.root_path().'think workerman reload');

            (new CloudService())->setProcessLog($data['client_id'], '删除临时目录');
            // 删除目录
            delete_directory(public_path() . 'part' . DIRECTORY_SEPARATOR . $data['client_id']);
            (new CloudService())->setProcessLog($data['client_id'], '下载成功', '');

            $actionType = $data['action_type'];
       
            $pluginService = new PluginService();
            // 更新系统
            if($actionType == 'updateSystem') {
                $pluginService->updateSystem();
            }
            // 安装插件
            if ($actionType == 'installPlugin') {
                $pluginService->install($data['plugin']['name'], $data['plugin']['version']);
            }

            // 卸载插件
            // if($actionType == 'uninstallPlugin'){
            //     $pluginService->uninstall($data['plugin']['name']);
            // }

            // 更新插件
            if ($actionType == 'updatePlugin') {
                $pluginService->update($data['plugin']['name'], $data['plugin']['version']);
            }

            // 删除打包文件
            //如果任务执行成功后 记得删除任务，不然这个任务会重复执行，直到达到最大重试次数后失败后，执行failed方法
          

            (new CloudService())->setProcessLog($data['client_id'], '完成', 'exit');
         
            // 也可以重新发布这个任务
            // $job->release($delay); //$delay为延迟时间
        } catch (\Exception $e) {
             copy($adminBackupFile, root_path(). 'config'. DIRECTORY_SEPARATOR.'admin.php');
             (new CloudService())->setProcessLog($data['client_id'],'发生错误：' . $e->getMessage().$e->getFile(),'error');
        }


    }

    public function failed($data)
    {

        // ...任务达到最大重试次数后，失败了

    }



}